package cn.xz.chess.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import cn.xz.chess.MainActivity;
import cn.xz.chess.R;

public class TabSettingsActivity extends TabActivity implements View.OnClickListener {
    RadioGroup mGroup;

    private static int getRealId(int id) {
        switch (id) {
            case R.id.cs:
                return 3;
            case R.id.en:
                return 4;
            case R.id.es:
                return 5;
            case R.id.ca:
                return 6;
            case R.id.localedefault:
                return 1;
            case R.id.sdcard:
                return 2;
            default:
                return 1;
        }
    }

    private static int getId(int realId) {
        switch (realId) {
            case 3:
                return R.id.cs;
            case 4:
                return R.id.en;
            case 5:
                return R.id.es;
            case 6:
                return R.id.ca;
            case 2:
                return R.id.sdcard;
            default:
                return R.id.localedefault;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("SETTINGS");
        TabHost tabHost = getTabHost();
        LayoutInflater.from(this).inflate(R.layout.settings, tabHost.getTabContentView(), true);
        tabHost.addTab(tabHost.newTabSpec("level").setIndicator("LEVEL").setContent(R.id.level));
        tabHost.addTab(tabHost.newTabSpec("locale").setIndicator("LOCALE").setContent(R.id.locale));
        setResult(11, new Intent());

        // TAB locale
        Button ok = (Button) findViewById(R.id.ok);
        mGroup = (RadioGroup) findViewById(R.id.localegroup);
        SharedPreferences pref = getBaseContext().getSharedPreferences(MainActivity.SETTINGS, 0);
        RadioButton selected = (RadioButton) findViewById(getId(pref.getInt(MainActivity.LOCALE, 1)));
        selected.setChecked(true);
        ok.setText("OK");
        RadioButton cs = (RadioButton) findViewById(R.id.cs);
        RadioButton en = (RadioButton) findViewById(R.id.en);
        RadioButton es = (RadioButton) findViewById(R.id.es);
        RadioButton ca = (RadioButton) findViewById(R.id.ca);
        RadioButton def = (RadioButton) findViewById(R.id.localedefault);
        cs.setText("CZECH");
        en.setText("ENGLISH");
        es.setText("SPANISH");
        ca.setText("CATALAN");
        def.setText("DEFAULT");
        ok.setOnClickListener(this);

        // TAB level
        Button oklevel = (Button) findViewById(R.id.oklevel);
        oklevel.setText("确定");
        oklevel.setOnClickListener(this);
        TextView levelstatic = (TextView) findViewById(R.id.levelstatic);
        levelstatic.setText("AI思考时间");
        EditText time = (EditText) findViewById(R.id.time);
        time.setText(new Integer(pref.getInt(MainActivity.TIME_PER_MOVE, 5000)).toString());
    }

    public void onClick(View v) {
        int iTime = 1;
        EditText time = (EditText) findViewById(R.id.time);
        try {
            iTime = Integer.valueOf(time.getText().toString());
        } catch (NumberFormatException n) {
            return;
        }
        SharedPreferences pref = getBaseContext().getSharedPreferences(MainActivity.SETTINGS, 0);
        SharedPreferences.Editor editor = pref.edit();

        int type;
        editor.putInt(MainActivity.LOCALE, type = getRealId(mGroup.getCheckedRadioButtonId()));
        editor.putInt(MainActivity.TIME_PER_MOVE, iTime);
        editor.commit();
        finish();
    }

}
